<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<style>
    td input {
        width: 100%;
        line-height: 1.5;
        vertical-align: top;
        border-top: 2px solid #f7f7f7; /*边框颜色*/
        outline: none;
    }
</style>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-purchases-edit">
        <!--需要批量的id集合-->
        <input name="ids" id="ids" type="hidden" th:value="${ids}">
        <!--需要的名字-->
        <input name="names" id="names_tem" class="name_tem" type="hidden" th:value="${names}">
        <div id="table_list">

        </div>
        <div class="form-group">
            <div class="form-control-static col-sm-offset-9">
                <button type="submit" class="btn btn-primary">submit</button>
                <button onclick="$.modal.close()" class="btn btn-danger" type="button">close</button>
            </div>
        </div>
    </form>
</div>
<div th:include="include::footer"></div>
<script th:src="@{/ajax/libs/select/select2.js}"></script>
<script type="text/javascript">
    var prefix = ctx + "module/fbsHead";

    $("#form-purchases-edit").validate({
        submitHandler: function (form) {
            edit();
        }
    });

    function edit () {

        var j=0;
        var ids='';
        var quantity = '';
        var actualQuantity = new Array();

        //获取id集合
        $(".names").each(function () {
            var tablename = '#bootstrap-table'+j
            ids += getBootstrapTableValueAsTwo('id',tablename) + ","
            quantity += getBootstrapTableValueAsTwo('quantity',tablename) + ","
            j++;
        })

        var reg = new RegExp("^[0-9]*$");
        var i=0;
        var quantity_tem = quantity.split(",");
        $(".actualQuantity").each(function () {
            var tem_var = $(this).val().trim()

            if (parseInt(tem_var) <= parseInt(quantity_tem[i]) && reg.test(tem_var) && objIsNull(tem_var) ){
                actualQuantity.push(tem_var);
            }else {
                actualQuantity.push('0');
            }
            i++;
        });

         $.ajax({
             type: "POST",
             url: ctx + "module/fbsHead/edit_received",
             data: {
                 "ids": ids,
                 "actualQuantity": actualQuantity,
                 "headIds":$('#ids').val()
             },
             async: false,
             error: function (request) {
                 $.modal.alertError("system error");
             },
             success: function (data) {
                 $.operate.saveSuccess(data);
             }
         });
    }



    $(function () {

        var ids = $('#ids').val().split(",");
        var name_tem = $('#names_tem').val().split(",");

        var i = 0;

        ids.forEach(function(e){
            var table_name = "'"+i+"'";
            var table_name1 = i;

            $('#table_list').append("<div class=\"form-group\">\n" +
                "                <label class=\"col-sm-3 control-label\">名称：</label>\n" +
                "                <div class=\"col-sm-8\">\n" +
                "                    <input id=\"names\" disabled name=\"names\" value='"+name_tem[i]+"' class=\"names form-control\" type=\"text\">\n" +
                "                </div>\n" +
                "            </div>" +
                "<table id=\"bootstrap-table"+i+"\" class=\"table text-nowrap\" data-show-columns=\"true\" data-mobile-responsive=\"true\"></table>")

            var options = {
                url: prefix + "/received_list",
                modalName: "海外仓签收",
                search: false,
                queryParams: {
                    ids: e
                },
                showRefresh: false,
                showExport: false,
                showToggle: false,
                showColumns: false,
                showFooter:true,
                table_id:'#bootstrap-table'+i,
                columns: [
                    {
                        field: 'id',
                        align: 'center',
                        title: 'id',
                        visible: false
                    },
                    {
                        title: 'serialNumber',
                        align: 'center',
                        width: '40',
                        formatter: function (value, row, index) {
                            var pageSize = $(options.table_id).bootstrapTable('getOptions').pageSize;     //通过table的#id 得到每页多少条
                            var pageNumber = $(options.table_id).bootstrapTable('getOptions').pageNumber; //通过table的#id 得到当前第几页
                            return pageSize * (pageNumber - 1) + index + 1;    // 返回每条的序号： 每页条数 *（当前页 - 1 ）+ 序号
                        },
                        footerFormatter: function (value) {
                            return "total";
                        }
                    },
                    {
                        field: 'sku',
                        align: 'center',
                        title: 'SKU'
                    },
                    {
                        field: 'chineseName',
                        align: 'center',
                        title: 'productName'
                    },
                    {
                        field: 'quantity',
                        align: 'center',
                        width: '40',
                        title: 'quantityShipped',
                        footerFormatter: function (value) {
                            return calculateLineTotal(value,'quantity');
                        }
                    },
                    {
                        field: 'actualQuantity',
                        align: 'center',
                        width: '40',
                        title: 'realQuantity',
                        formatter: function (value, row, index) {
                            var actions = [];
                            if (!objIsNull(value)){
                                value = row.quantity;
                            }
                            actions.push('<input type="text" onBlur="sum_sj('+table_name+')" value="' + value + '" class="actualQuantity no_input"/>');
                            return actions.join('');
                        },
                        footerFormatter: function (value) {
                            return sum_sj(table_name1);
                        }
                    }]
            };
            $.table.init(options);

            i++;
        });

    });

    function sum_sj(i) {
        var table_name = "[id='bootstrap-table"+i+"']";
        var sum = 0;
        $(table_name+" input.actualQuantity").each(function () {
            sum += parseInt($(this).val());
            $(".fixed-table-footer:eq("+i+") > table > tbody > tr > td:nth-child(5) > div.th-inner").html(sum)
        });
        return sum;
    }
</script>
</body>
</html>
